﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Login.Common
{
    public class Encryp
    {
        /// <summary>
        /// DES加密密钥
        /// </summary>
        public const string DESKey = "des!@#+1";

        /// <summary>
        /// 16未小写MD5算法
        /// </summary>
        /// <param name="ConvertString"></param>
        /// <returns></returns>
        public static string GetMd5String16Low(string ConvertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string converted = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
            converted = converted.Replace("-", "");
            converted = converted.ToLower();
            return converted;
        }


        //public static string DesEncrypt(string source)
        //{
        //    byte[] bykey = null;
        //    byte[] IV = { 0x34, 0x12, 0x90, 0x56, 0x78, 0xAB, 0x69, 0x69 };
        //    bykey = Encoding.Default.GetBytes(m_EncrypKey);
        //    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        //    byte[] inputByteArray = Encoding.Default.GetBytes(source);
        //}

        public static string DesEncrypt(string dataToEncrypt)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] IV = { 0x34, 0x12, 0x90, 0x56, 0x78, 0xAB, 0x69, 0x69 };
                byte[] inputByteArray = Encoding.Default.GetBytes(dataToEncrypt);//把字符串放到byte数组中
                des.Key = ASCIIEncoding.ASCII.GetBytes(DESKey); //建立加密对象的密钥和偏移量
                des.IV = IV;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        StringBuilder ret = new StringBuilder();
                        foreach (byte b in ms.ToArray())
                        {
                            ret.AppendFormat("{0:x2}", b);
                        }
                        return ret.ToString();
                    }
                }
            }
        }

        public static string DesDecrypt(string dataToDecrypt)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] IV = { 0x34, 0x12, 0x90, 0x56, 0x78, 0xAB, 0x69, 0x69 };
                byte[] inputByteArray = new byte[dataToDecrypt.Length / 2];
                for (int x = 0; x < dataToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(dataToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                des.Key = ASCIIEncoding.ASCII.GetBytes(DESKey); //建立加密对象的密钥和偏移量，此值重要，不能修改
                des.IV = IV;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        return System.Text.Encoding.Default.GetString(ms.ToArray());
                    }
                }
            }
        }

    }
}